<?php

class promotion_helper
{
	/**
	 * 获取加价购商品列表
	 */
    public static function get_promotion_add_list()
    {
        $list = _model('promotion_add')->getList(array('id>'=>0));

        if ($list) {
            foreach ($list as $k=>$v) {
                $sku_ids = goods_helper::get_sku_ids_by_goods_id($v['goods_id'], 1);
                $new_ids = array();
                foreach ($sku_ids as $value) {
                    $sku_info = _uri('sku', $value);
                    if ($sku_info['stock'] > 0) {
                        $new_ids[] = $value;
                    }
                }
                
                $goods_info = _uri('goods', array('id'=>$v['goods_id']));
                if (empty($new_ids) || !$goods_info['is_sale']) {
                    unset($list[$k]);
                    continue;
                }
                $list[$k]['title']   = $goods_info['title'];
                $list[$k]['price']   = $goods_info['price'];
                $list[$k]['cover']   = goods_helper::get_goods_cover($v['goods_id']);
                $list[$k]['sku_ids'] = join(',', $new_ids);
                $list[$k]['sku_num'] = count($new_ids);
            }

            return array_values($list);
        }

        return array();
    }

    /**
     * 是否是加价购
     * @param $goods_id
     * @return bool
     */
    public static function is_relation($goods_id)
    {
        if (!$goods_id) {
            return false;
        }

        $info = _uri('promotion_add', array('goods_id'=>$goods_id));

        if (!empty($info)) {
            return true;
        }

        return false;
    }

    /**
     * 获取 满减优惠列表
     *
     */
    public static function get_promotion_sub_list()
    {
    	$filter = array(
    			'`start_time` <= ' => date('Y-m-d', time()).'23:59:59',
				'`end_time` >= '   => date('Y-m-d', time()).'00:00:00',
				'status'           => 1
    		);
    	$list = _model('promotion_sub')->getList($filter, ' ORDER BY `limit_price` DESC ');

    	if ($list) {
    		return $list;
    	}

    	return array();
    }

    /**
     * 返回当前优惠信息
     */
    public static function get_promotion_info($price)
    {
        $info = array();
        $list = self::get_promotion_sub_list();
        if (!$list || empty($list)) {
            return $info;
        }
        foreach ($list as $k=>$v) {
            if ($price >= $v['limit_price']) {
                $info = $v;
                break;
            }
        }

        return $info;
    }

    /**
     * 获取当前满减优惠
     */
    public static function get_curr_promotion($price)
    {
    	$info = self::get_promotion_info($price);
    	if (!$info || empty($info)) {
    		return 0;
    	}
    	
        return $info['offer_price'];
    }
}